package com.nenglai.admin.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;

/**
 * @author kongdehui
 * @date 2021/06/29 19:17
 **/
@Component
public class COSClientUtil {

    // @Value("${cos.secret-id}")
    private String secretId = "AKIDb091cwzlGVTAajbCWZMs75nEOGrCG3Zp";

    // @Value("${cos.secret-key}")
    private String secretKey = "04rOedLmwpRmqd8MK1RaS4PXOzJL6bpm";

    // @Value("${cos.bucket-name}")
    private String bucketName = "automatic-report-1257614471";

    /**
     *
     */
    private COSClient cosClient;

    @PostConstruct
    public void init() {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-nanjing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        cosClient = new COSClient(cred, clientConfig);
    }

    /**
     * 文件
     *
     * @param key
     * @param file
     * @return com.qcloud.cos.model.PutObjectResult
     * @author kongdehui
     * @date 2021/06/29 20:07
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 输入流
     *
     * @param key
     * @param inputStream
     * @param metadata
     * @return com.qcloud.cos.model.PutObjectResult
     * @author kongdehui
     * @date 2021/06/29 20:07
     */
    public PutObjectResult putObject(String key, InputStream inputStream, ObjectMetadata metadata) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * @param key
     * @return void
     * @author kongdehui
     * @date 2021/06/29 21:34
     */
    public void deleteObject(String key) {
        cosClient.deleteObject(bucketName, key);
    }

    /**
     * @param key
     * @return com.qcloud.cos.model.COSObjectInputStream
     * @author kongdehui
     * @date 2021/06/29 22:00
     */
    public FileInputStream getObject(String key, File file) throws IOException {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        cosClient.getObject(getObjectRequest, file);
        return new FileInputStream(file);
    }

    /**
     * 获取
     * @author kongdehui
     * @date 2021/07/02 13:22
     * @param key
     * @param file
     */
    public void getObjectFile(String key, File file) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        cosClient.getObject(getObjectRequest, file);
    }
}
